/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.future.projects.imobiliaria.core;

import com.future.projects.imobiliaria.api.MainService;
import com.future.projects.imobiliaria.core.exception.CoreException;
import com.future.projects.imobiliaria.dao.DAOFactory;
import com.future.projects.imobiliaria.dao.UsuarioDAO;
import com.future.projects.imobiliaria.exception.DAOException;
import com.future.projects.imobiliaria.model.Usuario;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author fernandofot
 */
public class MainServiceSimpleImpl implements MainService {

    private DAOFactory factory;

    public void setFactory(DAOFactory factory) {
        this.factory = factory;
    }

    public DAOFactory getFactory() {
        return factory;
    }

    @Override
    @Transactional
    @SuppressWarnings({"BroadCatchBlock", "TooBroadCatch"})
    public Long createUsuario(Usuario usuario) {
        UsuarioDAO dao = factory.getUsuarioDAO();

        try {
            Long id = dao.create(usuario);
            return id;
        } catch (DAOException ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException(ex.getMessage());
        } catch (Exception ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException("Erro: procure o Administrador");
        }

    }

    @Override
    @Transactional
    @SuppressWarnings({"BroadCatchBlock", "TooBroadCatch"})
    public void updateUsuario(Usuario usuario) {
        UsuarioDAO dao = factory.getUsuarioDAO();

        try {
           dao.update(usuario);
          
        } catch (DAOException ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException(ex.getMessage());
        } catch (Exception ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException("Erro: procure o Administrador");
        }

    }

    @Override
    @Transactional
    @SuppressWarnings({"BroadCatchBlock", "TooBroadCatch"})
    public void removeUsuario(Long usuarioId) {
        UsuarioDAO dao = factory.getUsuarioDAO();
        try {
            dao.remove(usuarioId);
        } catch (DAOException ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException(ex.getMessage());
        } catch (Exception ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException("Erro: procure o Administrador");
        }
    }

    @Override
    @SuppressWarnings({"BroadCatchBlock", "TooBroadCatch"})
    @Transactional(readOnly = false)
    public List<Usuario> searchUsuarios(String term) {
        UsuarioDAO dao = factory.getUsuarioDAO();
        try {
            return dao.searchUsuarios(term);
        } catch (DAOException ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException(ex.getMessage());
        } catch (Exception ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException("Erro: procure o Administrador");
        }
    }

    @Override
    @SuppressWarnings({"BroadCatchBlock", "TooBroadCatch"})
    @Transactional(readOnly = false)
    public List<Usuario> listUsuarios() {
        UsuarioDAO dao = factory.getUsuarioDAO();
        try {
            return dao.listUsuarios();
        } catch (DAOException ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException(ex.getMessage());
        } catch (Exception ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException("Erro: procure o Administrador");
        }
    }

    @Override
    @SuppressWarnings({"BroadCatchBlock", "TooBroadCatch"})
    @Transactional(readOnly = false)
    public Usuario findUsuario(Long usuarioId) {
        UsuarioDAO dao = factory.getUsuarioDAO();
        try {
            return dao.find(usuarioId);
        } catch (DAOException ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException(ex.getMessage());
        } catch (Exception ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException("Erro: procure o Administrador");
        }
    }

}
